package ThreadDemo2;

/**
 * Created with IntelliJ IDEA.
 * Description:线程安全问题示例
 * User: 阿宾
 * Date: 2025-03-21
 * Time: 08:06
 */
public class ThreadDemo2 {
    private static int count = 0;

    public static void main(String[] args) throws InterruptedException {
        //创建两个线程，每个线程对count加5W次
        Thread t1 = new Thread(
                ()->{
                    for (int i = 0; i < 50000; i++) {
                        count++;
                    }
                }
        );
        Thread t2 = new Thread(
                ()->{
                    for (int i = 0; i < 50000; i++) {
                        count++;
                    }

                }
        );
        t1.start();

        t2.start();
        t1.join();
        t2.join();
        //正确结果应该是10W
        System.out.println("count = "+count);
    }
}
